package com.cn.aam.checaiduo.ui.splash;

import android.os.CountDownTimer;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListV2Listener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.bean.BannerB;
import com.cn.aam.checaiduo.bean.base.PageB;
import com.cn.aam.checaiduo.bean.shareference.AppSharePre;
import com.cn.aam.checaiduo.center.SharePreferenceCenter;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.utils.MyViewsUtils;
import com.flyco.roundview.RoundTextView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 启动页
 * monker on 2017/5/22.
 */
public class SplashActivity extends BaseActivity {
    private final static int COUNT_DOWN_AD = 5;

    @BindView(R.id.btn_jump_out)
    RoundTextView btnJumpOut;
    @BindView(R.id.iv_ad)
    ImageView ivAd;

    SharePreferenceCenter sharePreferenceCenter = SharePreferenceCenter.getInstance();


    private boolean isFinishPage = false;


    private CountDownTimer cd;

    @Override
    protected int layoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onGenerate() {
        initSharePrefence();
        startCountTime();
        UserCenter.getInstance().clearUserM();
        //根据具体ip获取host
//        RetrofitHomeS.getInstance()
//                .getSpecialAPi()
//                .getSpecialHost()
//                .compose(RxSchedulers.<IPB>toMain())
//                .subscribe(new OnRequestListener<IPB>(this) {
//                    @Override
//                    protected boolean isShowProgressBar() {
//                        return false;
//                    }
//
//                    @Override
//                    public void onNext(IPB value) {
//                        super.onNext(value);
//                        RetrofitHome.HOST_API = BuildConfig.DEBUG ? value.getTest_url() : value.getProduct_url();
////                        RetrofitHome.HOST_API = BuildConfig.DEBUG ? value.getProduct_url() : value.getTest_url();
//                        getAdInfo();
//                    }
//                });
        getAdInfo();

    }

    private void getAdInfo() {
        RetrofitHome.getInstance()
                .getInternalApi()
                .getBannerList("4")
                .compose(RxSchedulers.<PageB<BannerB>>toMain())
                .subscribe(new OnRequestListV2Listener<BannerB>(this) {
                    @Override
                    protected boolean isShowProgressBar() {
                        return false;
                    }

                    @Override
                    public void onNext(PageB<BannerB> value) {
                        super.onNext(value);
                        if (value.getCode() == Const.REQUEST_SUCCESS) {
                            String url = value.getData().get(0).getBanner();
                            //url = "https://www.youduo.vip/image/loading/adx2.jpg";
                            /**
                             * 尺寸
                             * 640x960
                             * 640x1136
                             * 750x1334
                             * 1242x2208
                             */
                            Glide.with(SplashActivity.this).load(url + "7501334.png").asBitmap().placeholder(R.drawable.img_start_page).into(ivAd);
                        }

                    }
                });
    }

    private void initSharePrefence() {
        //初始化APP各种配置
        if (sharePreferenceCenter.getAppSharePre() == null)
            sharePreferenceCenter.setAppSharepre(new AppSharePre());

        AppData.isGestureVerify = false;
    }


    /**
     * 开始倒计时
     */
    private void startCountTime() {
        btnJumpOut.setText(COUNT_DOWN_AD + "s " + "跳过");
        //倒计时
        cd = new CountDownTimer(5 * 1000, 1000) {
            @Override
            public void onTick(long l) {
                btnJumpOut.setText(l / 1000 + "s " + "跳过");
            }

            @Override
            public void onFinish() {
                btnJumpOut.setText("0s " + "跳过");
                dispatchGo();
            }
        };
        cd.start();
    }

    /**
     * 结束倒计时
     */
    private void endCountTime() {
        if (cd != null) {
            cd.cancel();
            cd = null;
        }
    }


    /**
     * 立即体验
     */
    @OnClick(R.id.btn_jump_out)
    public void onClick() {
        if (MyViewsUtils.isFastDoubleClick()) return;
        dispatchGo();
    }

    /**
     * 路径分发
     */
    private synchronized void dispatchGo() {
        if (isFinishPage) {
            return;
        }
        isFinishPage = true;
        endCountTime();
        //进入首页
        gotoMainPage();
        finish();
        //进入引导页
        AppSharePre appSharePre = sharePreferenceCenter.getAppSharePre();
        if (!appSharePre.isFirstLaunchApp()) {
            gotoIntroducePage();
            appSharePre.setFirstLaunchApp(true);
            sharePreferenceCenter.setAppSharepre(appSharePre);
        }
    }


    //进入首页
    private void gotoMainPage() {
        UIRouter.getInstance().openPage(mContext, UIRouter.HOME.AC_MAIN);
    }

    //进入介绍页面
    private void gotoIntroducePage() {

    }

    @Override
    public void onBackPressed() {
        //屏蔽返回按钮
    }
}
